//
//  CLMessage.m
//  RuntimeTest
//
//  Created by Charles on 16/9/26.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLMessage.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <UIKit/UIKit.h>


@implementation CLMessage

- (void)noArgumentsNoReturnValue{
    NSLog(@"%s was called, and it has no arguments and return value",__func__);
}

- (void)hasArguments:(NSString *)arg{
    NSLog(@"%s was called, and argument is %@", __FUNCTION__, arg);
}

- (NSString *)noArgumentsHasReturnValue{
    NSLog(@"%s was called, and return value is %@", __FUNCTION__, @"不带参数，但是带有返回值");
    return @"不带参数，但是带有返回值";
}

- (int)hasArguments:(NSString *)arg returnValue:(int)arg1{
    NSLog(@"%s was called, and argument is %@, return value is %d", __FUNCTION__, arg, arg1);
    return arg1;
}

int cFuc(id receiver,SEL sel,const void * arg1, const void * arg2){
    NSLog(@"%s was called, arg1 is %@, and arg2 is %@",
          __FUNCTION__,
          [NSString stringWithUTF8String:arg1],
          [NSString stringWithUTF8String:arg1]);
    return 1;
}

- (float)returnFloatType {
    NSLog(@"%s was called, and has return value", __FUNCTION__);
    return 3.121592612321323123;
}

- (CGRect)returnTypeIsStruct {
    NSLog(@"%s was called", __FUNCTION__);
    
    return CGRectMake(0, 0, 10, 10);
}


+ (void)test{
    //1.创建对象
    CLMessage * msg = ((CLMessage *(*)(id,SEL))objc_msgSend)((id)[CLMessage class],@selector(alloc));
    //2.初始化对象
    msg = ((CLMessage *(*)(id, SEL))objc_msgSend)((id)msg,@selector(init));
    //3.调用无参数无返回值方法
    ((void (*)(id,SEL))objc_msgSend)((id)msg,@selector(noArgumentsNoReturnValue));
    //4.调用带一个参数但无返回值的方法
    ((void(*)(id,SEL,NSString *))objc_msgSend)((id)msg,@selector(hasArguments:),@"Charles");
    //5.调用带回返回值,但是不带参数
    NSString * returnValue = ((NSString *(*)(id,SEL))objc_msgSend)((id)msg,@selector(noArgumentsHasReturnValue));
    NSLog(@"returnValue is %@",returnValue);
    //6.带参数带返回值
    int returnVal =  ((int(*)(id,SEL,NSString *,int))objc_msgSend)((id)msg,@selector(hasArguments:returnValue:),@"Charles",1024);
    NSLog(@"returnVal is %d",returnVal);
}

@end
